import { check, body,delJson, getJson, postJson, parseJSON, checkStatus,checkCallback, data, putJson } from './util'
import { success } from '../ui-components'
import util from '../util'
import { createAction } from 'redux-actions'

export const fetchSurvey = createAction('fetch survey', args =>
  fetch(`/surveys/items?${util.body(args)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
);

export const fetchSurveyConfig = createAction('fetch survey config', () =>
  fetch(`/surveys/configs`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
);

export const resetSurvey = createAction('reset survey');

export const updateSurveyConfig = createAction('update survey config', (arg, cb) =>
  fetch(`/surveys/configs`, {
    ...putJson,
    ...body({
      ...arg,
    }),
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
);

export const addSurveyItem = createAction('add survey item', (item, cb) =>
  fetch(`/surveys/items`, {
    ...postJson,
    ...body({
      ...item
    }),
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
);

export const updateSurveyItem = createAction('_', (itemId, item, cb) =>
  fetch(`/surveys/items/${itemId}`, {
    ...putJson,
    ...body({
      ...item
    }),
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
);

export const fetchSamePeriodItems = createAction('_', (startTime, endTime, options) =>
  fetch(`/surveys/items/same-period?${util.body({startTime, endTime})}`, getJson)
  .then(res => checkCallback(res, options))
);

export const fetchSurveyActivity = createAction('fetch survey activity', () =>
  fetch(`/surveys/activities/in-period`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
);

export const deleteSurveyItem = createAction('_', (itemId, cb) =>
  fetch(`/surveys/items/${itemId}`, delJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
);

export const submitSurveyActivity = createAction('_', (arg, cb) =>
  fetch(`/surveys/activities/submit`, {
    ...putJson,
    ...body(arg),
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
);

export const fetchSurveyActivities = createAction('_', (pageNumber, cb) =>
  fetch(`/surveys/activities?pageNumber=${pageNumber}&pageSize=50`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
);

export const fetchSurveyActivityItems = createAction('_', (activityId, cb) =>
  fetch(`/surveys/activities/${activityId}/items`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
);

export const fetchSurveyHistoryItems = createAction('_', (startTime, endTime, cb) => {
 return (
    fetch(`/surveys/activities/items/history-charts?${util.body({ endTime, startTime })}`, getJson)
       .then(checkStatus)
        .then(parseJSON)
       .then(check)
       .then(cb)
   );
 });
